package com.prs.crm.job.impl;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLEncoder;
import java.util.Collection;

import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.quartz.StatefulJob;

import com.prs.crm.context.Constrains;
import com.prs.crm.context.ToolKits;
import com.prs.crm.domain.sms.SmsSend;
import com.prs.crm.job.SmsSendJob;
import com.prs.crm.service.sms.SmsSendService;

public class SmsSendJobImpl implements SmsSendJob,StatefulJob{
	private SmsSendService smsSendService;
	
	public void execute(JobExecutionContext context) throws JobExecutionException
	{
	
		if (smsSendService==null)
			smsSendService= (SmsSendService)context.getJobDetail().getJobDataMap().get("smsSendService");
		
		this.sendServer();
	}


  private void sendServer() {
		Collection<SmsSend> smsMts = smsSendService.getSendingSms();
		for(SmsSend smsMt : smsMts) {
			String operator = ToolKits.getOperator(smsMt.getTelephoneNo());
			

			try {
				int result = this.sendSMS(smsMt.getTelephoneNo(), smsMt.getMessage(), "");
				
				smsMt.setClientName("普睿思EIM");				
				smsMt.setOperator(operator);
				smsMt.setSendPlat("短信平台");
				smsMt.setFlag(String.valueOf(result));
				if(result == 0) {
					smsMt.setState(Constrains.SMS_SUCCESS);
					smsMt.setDescribeInfo("发送成功");
				}else if(result == -1) {
					smsMt.setState(Constrains.SMS_FAIL);
					smsMt.setDescribeInfo("帐号未注册");
				}else if(result == -2) {
					smsMt.setState(Constrains.SMS_FAIL);
					smsMt.setDescribeInfo("其他错误");
				}else if(result == -3) {
					smsMt.setState(Constrains.SMS_FAIL);
					smsMt.setDescribeInfo("密码错误");
				}else if(result == -4) {
					smsMt.setState(Constrains.SMS_FAIL);
					smsMt.setDescribeInfo("手机号格式不对");
				}else if(result == -5) {
					smsMt.setState(Constrains.SMS_FAIL);
					smsMt.setDescribeInfo("余额不足");
				}else if(result == -6) {
					smsMt.setState(Constrains.SMS_FAIL);
					smsMt.setDescribeInfo("定时发送时间不是有效的时间格式");
				}

				this.smsSendService.save(smsMt);
			} catch (Exception e) {
				// TODO: handle exception
				e.printStackTrace();
			}
		
		}
	}

	
	/*
	 * 发送方法  其他方法同理
	 * 输入参数：
	 * CorpID-帐号，
	 * Pwd-密码，
	 * Mobile-发送手机号，
	 * Content-发送内容，
	 * Cell-子号(可为空），
	 * SendTime-定时发送时间(固定14位长度字符串，比如：20060912152435代表2006年9月12日15时24分35秒，可为空)
	 * 输出参数：整数
	        0，发送成功；
	        -1、帐号未注册；
	        -2、其他错误；
	        -3、密码错误；
	        -4、手机号格式不对；
	        -5、余额不足；
	        -6、定时发送时间不是有效的时间格式；
	 * 注意：禁止10小时以内向同一手机号发送相同短信；
	 */
	
	private int sendSMS(String Mobile,String Content,String send_time) throws MalformedURLException, UnsupportedEncodingException {
		URL url = null;
		String CorpID="LINKT00092";//账户名
		String Pwd="658422";//密码
		String send_content=URLEncoder.encode(Content.replaceAll("<br/>", " "), "GBK");//发送内容
		url = new URL("  http://mb345.com/WS/Send.aspx?CorpID="+CorpID+"&Pwd="+Pwd+"&Mobile="+Mobile+"&Content="+send_content+"&Cell=&SendTime="+send_time);
		BufferedReader in = null;
		int inputLine = 0;
		try {
			System.out.println("开始发送短信手机号码为 ："+Mobile);
			in = new BufferedReader(new InputStreamReader(url.openStream()));
			inputLine = new Integer(in.readLine()).intValue();
		} catch (Exception e) {
			System.out.println("网络异常,发送短信失败！");
			inputLine=-9;
		}
		System.out.println("结束发送短信返回值：  "+inputLine);
		return inputLine;
	}
}
